//
//  GMUserNameViewController.m
//  BeautyFace
//
//  Created by steven on 2017/1/3.
//  Copyright © 2017年 steven. All rights reserved.
//

#import "GMUserNameViewController.h"
#import "GMUserInfoDataController.h"

@interface GMUserNameViewController ()

@property (nonatomic, strong)GMUserInfoDataController *userInfoDataController;

@end

@implementation GMUserNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.userInfoDataController = [[GMUserInfoDataController alloc] init];
    [self setupContentView];
}

#pragma mark - set up content view
- (void)setupContentView {
    [self.navigationItem setTitle:@"修改昵称"];
    [self.view addSubview:self.nickNameTextField];
    [self.nickNameTextField autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:40];
    [self.nickNameTextField autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:20];
    [self.nickNameTextField autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:20];
    
    [self.view addSubview:self.hintLabel];
    [self.hintLabel autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:20];
    [self.hintLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.nickNameTextField withOffset:10];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    UIBarButtonItem *saveBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveBarButtonItemPressed)];
    [self.navigationItem setRightBarButtonItem:saveBarButtonItem];
    
    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];
    self.nickNameTextField.text = name;
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    CALayer *bottomLayer = [[CALayer alloc] init];
    bottomLayer.borderColor = UIColorFromRGB(0x928cc2).CGColor;
    bottomLayer.borderWidth = 1;
    bottomLayer.frame = CGRectMake(0, self.nickNameTextField.frame.size.height, self.nickNameTextField.frame.size.width, 1);
    [self.nickNameTextField.layer addSublayer:bottomLayer];
}

#pragma mark - save button pressed
- (void)saveBarButtonItemPressed {
    [self.nickNameTextField resignFirstResponder];
    NSString *nickName = self.nickNameTextField.text;
    if (nickName.length == 0) {
        [self showAlertMessage:@"请输入昵称"];
    } else {
        NSDictionary *params = @{@"name":nickName};
        [self.userInfoDataController updateUserInfoWithParams:params callback:^(NSString *error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (error == nil) {
                    [self showAlertMessage:@"修改成功"];
                } else {
                    [self showAlertMessage:error];
                }
            });
        }];
    }
}

#pragma mark - nickName text field
- (UITextField *)nickNameTextField {
    if (!_nickNameTextField) {
        _nickNameTextField = [[UITextField alloc] init];
    }
    
    return _nickNameTextField;
}

#pragma mark - hint label
- (UILabel *)hintLabel {
    if (!_hintLabel) {
        _hintLabel = [[UILabel alloc] init];
        _hintLabel.font = [UIFont systemFontOfSize:11];
        _hintLabel.textColor = [UIColor grayColor];
        _hintLabel.text = @"好的名字可以让大家更容易记住你。";
    }
    
    return _hintLabel;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
